/*
 * ----------------------------------------------------------------------- Copyright 2012 - Alistair
 * Rutherford - www.netthreads.co.uk
 * -----------------------------------------------------------------------
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.garrigou.spaceship.scenes;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;

/**
 * Helper methods for traversing through scenes and actors.
 * 
 * I have put this stuff here to void having to subclass the base types from LibGdx anymore than I
 * have to.
 * 
 */
public class SceneHelper
{
  private static final Vector2 point = new Vector2();

  /**
   * Find hit class.
   * 
   * @param x
   *          Current x position.
   * @param y
   *          Current y position
   * @param group
   *          The starting Group.
   * @param targetClass
   *          The target class type.
   * 
   * @return The target or null if not found.
   */
  @SuppressWarnings("rawtypes")
  public static Actor hit(float x, float y, Group group, Class targetClass)
  {
    Array<Actor> children = group.getChildren();

    Actor hit = null;
    boolean found = false;
    int index = children.size - 1;
    while (!found && index >= 0)
    {
      Actor child = children.get(index);

      if (child.getClass().isAssignableFrom(targetClass))
      {
        toChildCoordinates(child, x, y, point);

        if (child.hit(point.x, point.y) != null)
        {
          found = true;
          hit = child;
        }
        else if (child instanceof Group)
        {
          child = hit(x, y, (Group) child, targetClass);
        }
      }

      index--;
    }

    return hit;
  }

  /**
   * Look for intersection between two actors.
   * 
   * I have defined this here as I can't extend the base classes Actor and Group.
   * 
   * @param x
   *          The x pos rectangle.
   * @param y
   *          The y pos rectangle.
   * @param width
   *          The rectangle width.
   * @param height
   *          The rectangle height.
   * @param group
   *          The group.
   * @param targetClass
   *          The target class.
   * @return Target class, null if no intersection.
   */
  @SuppressWarnings("rawtypes")
  public static Actor intersects(float x, float y, float width, float height, Group group,
                                 Class targetClass)
  {
    Array<Actor> children = group.getChildren();

    Actor hit = null;
    int index = children.size - 1;
    while (hit == null && index >= 0)
    {
      Actor child = children.get(index);

      toChildCoordinates(child, x, y, point);

      // If child is our target class then immediately check for
      // intersection
      if (child.getClass().equals(targetClass))
      {
        if (isIntersect(point.x, point.y, width, height, 0, 0, child.getWidth(), child.getHeight()))
        {
          hit = child;
        }
      }
      else if (child instanceof Group)
      {
        hit = intersects(point.x, point.y, width, height, (Group) child, targetClass);
      }

      index--;
    }

    return hit;
  }

  /**
   * Makes simple intersection check.
   * 
   * @param sourceX
   *          Source rectangle x pos.
   * @param sourceY
   *          Source rectangle x pos.
   * @param sourceWidth
   *          Source rectangle width.
   * @param sourceHeight
   *          Source rectangle height.
   * @param targetX
   *          Target rectangle x pos.
   * @param targetY
   *          Source rectangle y pos.
   * @param targetWidth
   *          Source rectangle width.
   * @param targetHeight
   *          Source rectangle height.
   * 
   * @return True if two rectangles intersect.
   */
  public static boolean isIntersect(float sourceX, float sourceY, float sourceWidth,
                                    float sourceHeight, float targetX, float targetY,
                                    float targetWidth, float targetHeight)
  {
    float left1 = sourceX;
    float left2 = targetX;
    float right1 = sourceX + sourceWidth;
    float right2 = targetX + targetWidth;

    float top1 = sourceY;
    float top2 = targetY;
    float bottom1 = sourceY + sourceHeight;
    float bottom2 = targetY + targetHeight;

    if (bottom1 < top2) return false;
    if (top1 > bottom2) return false;

    if (right1 < left2) return false;
    if (left1 > right2) return false;

    return true;
  }

  /**
   * Look for target hit of specified class.
   * 
   * TODO: Maybe remove this.
   * 
   * @param x
   *          Current x position.
   * @param y
   *          Current y position
   * @param stage
   *          The starting stage.
   * @param targetClass
   *          The target class type.
   * @return Target class or null if not found.
   */
  @SuppressWarnings("rawtypes")
  public static Actor hit(float x, float y, Stage stage, Class targetClass)
  {
    Group root = stage.getRoot();

    Array<Actor> children = root.getChildren();

    Actor hit = null;
    boolean found = false;
    int index = children.size - 1;
    while (!found && index >= 0)
    {
      Actor child = children.get(index);

      toChildCoordinates(child, x, y, point);

      Actor childHit = child.hit(point.x, point.y);

      if (childHit != null && childHit.getClass().isAssignableFrom(targetClass))
      {
        found = true;
        hit = childHit;
      }
      else
      {
        index--;
      }
    }

    return hit;
  }

  /**
   * Transforms the coordinates given in the child's parent coordinate system to the child
   * {@link Actor}'s coordinate system.
   * 
   * @param child
   *          the child Actor
   * @param x
   *          the x-coordinate in the Group's coordinate system
   * @param y
   *          the y-coordinate in the Group's coordinate system
   * @param out
   *          the output {@link Vector2}
   */
  private static void toChildCoordinates(Actor child, float x, float y, Vector2 out)
  {
    if (child.getRotation() == 0)
    {
      if (child.getScaleX() == 1 && child.getScaleY() == 1)
      {
        out.set(x - child.getX(), y - child.getY());
      }
      else
      {
        if (child.getOriginX() == 0 && child.getOriginY() == 0)
        {
          out.set((x - child.getX()) / child.getScaleX(), (y - child.getY()) / child.getScaleY());
        }
        else
        {
          out.set((x - child.getX() - child.getOriginX()) / child.getScaleX() + child.getOriginX(),
                  (y - child.getY() - child.getOriginY()) / child.getScaleY() + child.getOriginY());
        }
      }
    }
    else
    {
      final float cos = (float) Math.cos((float) Math.toRadians(child.getRotation()));
      final float sin = (float) Math.sin((float) Math.toRadians(child.getRotation()));

      if (child.getScaleX() == 1 && child.getScaleY() == 1)
      {
        if (child.getOriginX() == 0 && child.getOriginY() == 0)
        {
          float tox = x - child.getX();
          float toy = y - child.getY();

          out.x = tox * cos + toy * sin;
          out.y = tox * -sin + toy * cos;
        }
        else
        {
          final float worldOriginX = child.getX() + child.getOriginX();
          final float worldOriginY = child.getY() + child.getOriginY();
          float fx = -child.getOriginX();
          float fy = -child.getOriginY();

          float x1 = cos * fx - sin * fy;
          float y1 = sin * fx + cos * fy;
          x1 += worldOriginX;
          y1 += worldOriginY;

          float tox = x - x1;
          float toy = y - y1;

          out.x = tox * cos + toy * sin;
          out.y = tox * -sin + toy * cos;
        }
      }
      else
      {
        if (child.getOriginX() == 0 && child.getOriginY() == 0)
        {
          float tox = x - child.getX();
          float toy = y - child.getY();

          out.x = tox * cos + toy * sin;
          out.y = tox * -sin + toy * cos;

          out.x /= child.getScaleX();
          out.y /= child.getScaleY();
        }
        else
        {
          float srefX = child.getOriginX() * child.getScaleX();
          float srefY = child.getOriginY() * child.getScaleY();

          final float worldOriginX = child.getX() + child.getOriginX();
          final float worldOriginY = child.getY() + child.getOriginY();
          float fx = -srefX;
          float fy = -srefY;

          float x1 = cos * fx - sin * fy;
          float y1 = sin * fx + cos * fy;
          x1 += worldOriginX;
          y1 += worldOriginY;

          float tox = x - x1;
          float toy = y - y1;

          out.x = tox * cos + toy * sin;
          out.y = tox * -sin + toy * cos;

          out.x /= child.getScaleX();
          out.y /= child.getScaleY();
        }
      }
    }
  }

}
